package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataBase.Assignment;
import DataBase.AssignmentManager;
import DataBase.Datasource;
import DataBase.DatasourceManager;

public class AddAssignmentServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddAssignmentServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Assignment asmt = new Assignment();
		Datasource ds = new Datasource();
		
		ds.setDs_hostname(request.getParameter("ds_hostname"));
		ds.setDs_IP(Assignment.getIP(request.getParameter("ip1"), request.getParameter("ip2"), request.getParameter("ip3"), request.getParameter("ip4")));
		ds.setDs_port(Integer.parseInt(request.getParameter("ds_port")));
		if(DatasourceManager.select(request.getParameter("ds_hostname")).size()==0){
			DatasourceManager.insert(ds);
			asmt.setDs_ID(DatasourceManager.getAutoKey(ds));
			asmt.setAsmt_name(request.getParameter("asmt_name"));
			asmt.setAsmt_state("开启");
			asmt.setAsmt_ftpname(request.getParameter("asmt_ftpname"));
			asmt.setAsmt_ftppwd( request.getParameter("asmt_ftppwd"));
			asmt.setAsmt_path(request.getParameter("asmt_path"));
			asmt.setAsmt_frequency(Integer.parseInt(request.getParameter("asmt_frequency")));
		    asmt.setFu_ID(Integer.parseInt(request.getParameter("asmt_frequencyMain")));
		    String next = Assignment.getNextDate(request.getParameter("asmt_frequencyMain"), request.getParameter("asmt_frequencyDetail"));
		    asmt.setAsmt_next(next);
		    asmt.setAsmt_time(Assignment.getTime(request.getParameter("hour"),request.getParameter("minute")));
		    AssignmentManager.insert(asmt);
			RequestDispatcher dispatcher = request
			.getRequestDispatcher("/Jsp/html/user/success.html");
	        dispatcher.forward(request, response);
		}else{
			RequestDispatcher dispatcher = request
			.getRequestDispatcher("/Jsp/html/user/failure.html");
	        dispatcher.forward(request, response);			
		}
//		String 
		
		
        
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
 
}
